/*
 *  Copyright (c) 2025 Huawei Device Co., Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hadss_geometry_transition/hadss_geometry_transition.dart';

void main() {
  testWidgets('closeBuilder 正常渲染并响应点击', (WidgetTester tester) async {
    bool openCalled = false;
    bool isTrue = true;
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: GeometryTransition(
            closedBuilder: (context, open) {
              return GestureDetector(
                onTap: () {
                  openCalled = true;
                  open();
                },
                child: const Text('close'),
              );
            },
            openBuilder: (context, close) {
              return Scaffold(
                body: Center(
                  child: GestureDetector(
                    onTap: close,
                    child: const Text('open'),
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );

    expect(find.text('close'), findsOneWidget);
    await tester.tap(find.text('close'));
    await tester.pumpAndSettle();
    expect(openCalled, isTrue);
  });

  testWidgets('openBuilder 能弹出新页面并响应关闭（带动画）', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: GeometryTransition(
            closedBuilder: (context, open) {
              return GestureDetector(
                onTap: open,
                child: const Text('close'),
              );
            },
            openBuilder: (context, close) {
              return Scaffold(
                body: Center(
                  child: GestureDetector(
                    onTap: close,
                    child: const Text('open'),
                  ),
                ),
              );
            },
            tappable: true,
          ),
        ),
      ),
    );

    await tester.tap(find.text('close'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 250)); // 动画中间
    expect(find.text('open'), findsOneWidget);

    await tester.tap(find.text('open'));
    await tester.pumpAndSettle();
    expect(find.text('close'), findsOneWidget);
  });

  testWidgets('tappable=false 时直接跳转页面，无动画', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: GeometryTransition(
            closedBuilder: (context, open) {
              return GestureDetector(
                onTap: open,
                child: const Text('close'),
              );
            },
            openBuilder: (context, close) {
              return Scaffold(
                body: Center(
                  child: GestureDetector(
                    onTap: close,
                    child: const Text('open'),
                  ),
                ),
              );
            },
            tappable: false,
          ),
        ),
      ),
    );

    await tester.tap(find.text('close'));
    await tester.pumpAndSettle(); // 直接跳转，无需等待动画
    expect(find.text('open'), findsOneWidget);

    await tester.tap(find.text('open'));
    await tester.pumpAndSettle();
    expect(find.text('close'), findsOneWidget);
  });

  testWidgets('onOpen 和 onClose 回调被正确调用', (WidgetTester tester) async {
    bool isTrue = false;
    bool onCloseCalled = false;
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: GeometryTransition(
            closedBuilder: (context, open) {
              return GestureDetector(
                onTap: open,
                child: const Text('close'),
              );
            },
            openBuilder: (context, close) {
              return Scaffold(
                body: Center(
                  child: GestureDetector(
                    onTap: close,
                    child: const Text('open'),
                  ),
                ),
              );
            },
            onPushCallback: (data) => onCloseCalled = true,
          ),
        ),
      ),
    );

    await tester.tap(find.text('close'));
    await tester.pumpAndSettle();
    expect(onCloseCalled, isTrue);
  });
}
